package com.tianji.learning.mq;

import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.service.IInteractionReplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class LikeTimesChangeListener {

  @Autowired
    private IInteractionReplyService replyService;

  //交换机 队列 bindingkey
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConstants.Exchange.LIKE_RECORD_EXCHANGE,type=
            ExchangeTypes.TOPIC),//交换机
            value = @Queue(value = "qa.liked.times.queue",durable = "true"),//队列
            key = MqConstants.Key.QA_LIKED_TIMES_KEY //bindingKey
    ))
    public void listenLikedTimesDTO(List<LikedTimesDTO> dtoList){
       log.info("监听到回答或评论点赞数量变化");
       //批量更新点赞次数
        ArrayList<InteractionReply> replyList = new ArrayList<>(dtoList.size());
        for (LikedTimesDTO likedTimesDTO : dtoList) {
            InteractionReply reply = new InteractionReply();
            reply.setId(likedTimesDTO.getBizId());
            reply.setLikedTimes(likedTimesDTO.getLikedTimes());
            replyList.add(reply);
        }
        replyService.updateBatchById(replyList);
    }

}
